package com.qf.shopping.controll.activityControll;

import com.qf.shopping.dto.systemDTO.UserProfileDTO;
import com.qf.shopping.pojo.System.User;
import com.qf.shopping.pojo.activity.Activity;
import com.qf.shopping.service.AdminMangerService;
import com.qf.shopping.service.activityService.IAcitvityService;
import com.qf.shopping.ustils.UserUtils;
import com.qf.shopping.vo.activity.AcitvityVO;
import com.qf.shopping.vo.activity.constom.ActivityConstom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Mawentao
 * @Date: 2019/5/6 0006 12:04
 * @Description:
 */
@Controller
@RequestMapping("/activity")
@SessionAttributes("user")
public class ActivityControll {

    @Autowired
    private IAcitvityService iAcitvityService = null;

    @Autowired
    private AdminMangerService adminMangerService = null;

    /**
     * 加载活动列表
     * @param model
     * @return
     */
    @GetMapping("/activity")
    public String getList(Model model){
        List<AcitvityVO> activity = iAcitvityService.getActivityData(null);
        model.addAttribute("activity",activity);
        return "/activity/activity/list";
    }

    /**
     * 跳转到增加活动页面
     * @return
     */
    @GetMapping("/addactivity")
    public String toAddActivity(Model model,HttpSession session){
        List<AcitvityVO> activity = iAcitvityService.getActivityData(null);

        User user = UserUtils.getUser(session);
        model.addAttribute("user",user);
        model.addAttribute("activity",activity);
        return "/activity/activity/add";
    }

    /**
     * 新增活动
     * @param
     * @return
     */
    @PostMapping("/addactivity")
    public String addActivity(ActivityConstom activityConstom, HttpSession session){
        AcitvityVO acitvityVO = new AcitvityVO();

        acitvityVO.setActivityConstom(activityConstom);

        iAcitvityService.addActivity(acitvityVO,session);

        return "redirect:/activity/activity";
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @GetMapping("/deleteactivity/{id}")
    public String deleteActivity(@PathVariable("id")Long id){
        iAcitvityService.deleteActivity(id);
        return "redirect:/activity/activity";
    }

    /**
     * 跳转到更新页面,会显示当前的数据信息
     * @param
     * @return
     */
    @GetMapping ("/updateactivity/{id}")
    public String toUpdateActivity(Model model ,@PathVariable("id") Long id){
        Activity activityDate = iAcitvityService.getActivityDate(id);
        model.addAttribute("activity",activityDate);
        Integer Uid = activityDate.getCreateUid();
        UserProfileDTO user = adminMangerService.findUserById(Uid);
        model.addAttribute("user",user);

        List<AcitvityVO> activitys = iAcitvityService.getActivityData(null);
        model.addAttribute("activitys",activitys);
        return "/activity/activity/update";
    }

    /**
     * 更新活动
     * @param
     * @return
     */
    @PostMapping("/uppdateactivity")
    public String updateActivity(ActivityConstom activityConstom){
        AcitvityVO acitvityVO = new AcitvityVO();
        acitvityVO.setActivityConstom(activityConstom);
        iAcitvityService.updateActivity(acitvityVO);
        return "redirect:/activity/activity";
    }

    /**
     * 搜索功能
     * @param model
     * @param activityConstom
     * @return
     */
    @RequestMapping("/getactivity")
    public String getActivity(Model model,ActivityConstom activityConstom){
        AcitvityVO acitvityVO = new AcitvityVO();
        acitvityVO.setActivityConstom(activityConstom);
        List<AcitvityVO> activitys = iAcitvityService.getActivityDataLike(acitvityVO);
        model.addAttribute("activity",activitys);
        return "/activity/activity/list";
    }

    /**
     * 发布活动
     * @param id
     * @return
     */
    @GetMapping("/publishactivity/{id}")
    public String publishActivity(@PathVariable("id")Long id){
        iAcitvityService.publishActivity(id,2);
        return "redirect:/activity/activity";
    }

    /**
     * 取消发布
     * @param id
     * @return
     */
    @GetMapping("/notpublishactivity/{id}")
    public String notPublishActivity(@PathVariable("id")Long id){
        iAcitvityService.publishActivity(id,3);
        return "redirect:/activity/activity";
    }

    /**
     * 推荐
     * @param id
     * @return
     */
    @GetMapping("/topactivity/{id}")
    public String topActivity(@PathVariable("id")Long id){
        iAcitvityService.activityHomeTop(id,2);
        return "redirect:/activity/activity";
    }

    /**
     * 取消推荐
     * @param id
     * @return
     */
    @GetMapping("/nottopactivity/{id}")
    public String notTopActivity(@PathVariable("id")Long id){
        iAcitvityService.activityHomeTop(id,1);
        return "redirect:/activity/activity";
    }

}
